function highlightNavItem(element, event) {
  event.preventDefault(); // 阻止默认行为

  // Remove the 'active' class from all nav items
  var navItems = document.getElementsByClassName('nav-item');
  for (var i = 0; i < navItems.length; i++) {
    navItems[i].classList.remove('active');
  }

  // Add the 'active' class to the clicked nav item
  element.classList.add('active');

  // 存储当前导航项的 href 到 sessionStorage
  sessionStorage.setItem('currentNavItem', element.getAttribute('href'));

  // 页面跳转
  window.location.href = element.getAttribute('href');
}

// 在页面加载时检查 sessionStorage 中是否有存储的当前导航项
document.addEventListener('DOMContentLoaded', function() {
  var currentNavItem = sessionStorage.getItem('currentNavItem');
  if (currentNavItem) {
    // 根据存储的当前导航项设置高亮样式
    var navItem = document.querySelector('.nav-item[href="' + currentNavItem + '"]');
    if (navItem) {
      navItem.classList.add('active');
    }
  }
});
// 粘性定位
var prevScrollPos = window.pageYOffset;

window.addEventListener("scroll", function() {
  var currentScrollPos = window.pageYOffset;
  var navbar = document.getElementById("navbar");

  if (prevScrollPos > currentScrollPos && currentScrollPos <= 50) {
    navbar.style.top = "50px";
  } else {
    navbar.style.top = "0 "; /* 隐藏导航栏的高度 */
  }

  prevScrollPos = currentScrollPos;
});